package forum.servlet;

import forum.dao.UserDao;
import forum.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author haomin
 * @date 2022/07/03 08:34
 **/
@WebServlet("/logon.do")
public class LogonDoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String occupation = req.getParameter("occupation");
        String hobby = req.getParameter("hobby");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");

        UserDao userDao = new UserDao();
        User user = userDao.insert(username,password1,sex,Integer.parseInt(age),occupation,hobby);

        HttpSession session = req.getSession();
        session.setAttribute("currentUser",user);

        resp.sendRedirect("/");

    }
}